package com.superhelper.superhelper_web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.superhelper.superhelper_web.common.CommonHelper;
import com.superhelper.superhelper_web.controller.base.AbstractController;
import com.superhelper.superhelper_web.domain.Admin;
import com.superhelper.superhelper_web.domain.Resource;
import com.superhelper.superhelper_web.domain.Resp;

/**
 * 管理异步登录控制器
 * 
 * */
@Controller
@RequestMapping("admin")
public class LoginAjaxController extends AbstractController {

	/**
	 * 登录校验
	 * 
	 * @param username
	 *            用户名(必传)
	 * @param pwd
	 *            　　　　 密码　(必传)
	 * @param vcode
	 *            验证码 (可选)
	 * @param remenber
	 *            是否记住密码(可选)
	 * @param request
	 * @param response
	 * @return map 校验结果
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping("login.do")
	public Map loginAjax(String username, @RequestParam String pwd,
			String remenber, HttpServletRequest request,
			HttpServletResponse response) {
		return adminService.login(username, pwd, remenber, request, response);
	}

	/**
	 * 登录注册
	 * 
	 * @param username
	 *            用户名(必传)
	 * @param pwd
	 *            　　　　 密码　(必传)
	 * @param vcode
	 * @param request
	 * @param response
	 * @return map 校验结果
	 */
	@SuppressWarnings("rawtypes")
	@ResponseBody
	@RequestMapping("register.do")
	public Map register(String username, @RequestParam String pwd) {
		return adminService
				.register(username, pwd);
	}

	/**
	 * 退出系统
	 * 
	 * @return
	 */
	@RequestMapping("exit.do")
	public String exit() {
		adminService.exit();
		return "admin/login";
	}

	/**
	 * 进入主页
	 * 
	 * @return
	 */
	@RequestMapping("index.do")
	public String controllerCenter(ModelMap model, HttpServletRequest request) {
		Admin admin = CommonHelper.getSessionUser();// 从session中获取admin对象
		if (null != admin) {
			// String redirect = request.getParameter("redirect");
			List<Resource> list = adminService.getMenuListByUserId(admin
					.getId());
			model.put("menuList", list);
			model.addAttribute("admin", admin);
			// model.addAttribute("redirect", redirect);
			return "admin/index";
		}
		return "admin/login";
	}

	/**
	 * 进入注册页
	 * 
	 * @return
	 */
	@RequestMapping("register_e.do")
	public String regist(ModelMap model, HttpServletRequest request) {
		return "admin/register";
	}

	/**
	 * 主页左侧菜单
	 * 
	 * @return
	 */
	@RequestMapping("memu.do")
	public String memu(Integer pid, ModelMap model) {
		Admin admin = CommonHelper.getSessionUser();// 从session中获取admin对象
		if (null != admin) {
			List<Resource> list = adminService.getMenuListByUserId(admin
					.getId());
			model.put("menuList", list);
			return "admin/menu";
		}
		return "admin/login";
	}

}
